package UI;

import Srevet_.Srevet;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ContactPanel extends JPanel {
    private final Socket socket;
    private final String username;
    private JList<String> contactList;
    private DefaultListModel<String> listModel;


    public ContactPanel(Socket socket, String username) {
        this.username=username;
        this.socket=socket;
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 标题
        JLabel titleLabel = new JLabel("联系人", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        add(titleLabel, BorderLayout.NORTH);

        // 联系人列表
        listModel = new DefaultListModel<>();
        new ClientReader(socket,this).start();






        contactList = new JList<>(listModel);
        contactList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        contactList.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        contactList.setCellRenderer(new ContactListRenderer());

        // 添加选择监听器
        contactList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    String selectedContact = contactList.getSelectedValue();
                    if (selectedContact != null) {
                        // 这里可以触发聊天面板的更新
                        System.out.println("选择了: " + selectedContact);
                    }
                }
            }
        });

        JScrollPane scrollPane = new JScrollPane(contactList);
        add(scrollPane, BorderLayout.CENTER);

        // 搜索框
        JPanel searchPanel = new JPanel(new BorderLayout());
        JTextField searchField = new JTextField();
        searchField.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        searchPanel.add(new JLabel("搜索: "), BorderLayout.WEST);
        searchPanel.add(searchField, BorderLayout.CENTER);
        add(searchPanel, BorderLayout.SOUTH);
    }

    public void updateOnLineUsers(List<String> onLineUsers) {
        // 在EDT线程中更新UI
        SwingUtilities.invokeLater(() -> {
            listModel.clear();
            for (String user : onLineUsers) {
                if (!user.equals(username)) { // 不显示自己
                    listModel.addElement(user);
                }
            }
        });
    }
    // 自定义联系人列表渲染器
    private static class ContactListRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                      boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            label.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
            label.setIcon(new ImageIcon("user_icon.png")); // 实际使用时替换为真实图标路径
            return label;
        }
    }
}